<?php
namespace app\admin\controller;

use app\common\model\WechatUserModel;
use app\common\model\project_model\AlbumImgCheckModel;
use app\yxwadmin\controller\Admin;
use think\Db;
use think\Exception;

class AlbumImgCheck extends Admin
{

    public $error_msg = '';

    /**
     * 首页
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
        $this->assign('label_list', json_encode(AlbumImgCheckModel::labelList()));
        $this->assign('is_out_rules_list', AlbumImgCheckModel::IsOutRulesList());
        return $this->view->fetch();
    }

    /**
     * 初始化表格
     * 关键字搜索
     * 分页
     * @throws
     * @return mixed
     */
    public function getList()
    {
        $limit = input('limit','0');
        $offset = input('offset','0');
        $sortName = input('sortName','id');
        $sortOrder = input('sortOrder','desc');
        $keyword = input('keywords','');
        $is_out_rules = input('is_out_rules','');
        $lalel = input('label','');

        if ($lalel) {
            $where['label'] = $lalel;
        }
        if ($is_out_rules !== '') {
            $where['is_out_rules'] = $is_out_rules;
        }
        if (empty($where)) {
            $where = '1=1';
        }

        $model = new AlbumImgCheckModel();
        if ($keyword) {
            $count = $model->where($where)->where('open_id|img_url', 'like', '%' . $keyword . '%')->count();
            $list = $model
                ->where($where)
                ->where('open_id|img_url', 'like', '%' . $keyword . '%')
                ->order([$sortName => $sortOrder])
                ->limit($offset . ',' . $limit)
                ->select();
        } else {
            $count = $model->where($where)->count(); //查询满足要求的总记录数
            $list = $model
                ->where($where)
                ->order(array($sortName=>$sortOrder))
                ->limit($offset.','.$limit)
                ->select();
        }

        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }

    public function view() {
        $id = input('id/d');
        $model = new AlbumImgCheckModel();
        $Rs = $model->where(['id' => $id])->find();
        $this->assign('Rs', $Rs);
        return $this->fetch();
    }

    /**
     * 审核
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function lock()
    {
        $open_id = input('open_id', '');
        $id = input('id/d', '');
        $model = new AlbumImgCheckModel();
        $where = ['id'=>$id];
        $data = $model->where($where)->find();
        $is_out_rules = AlbumImgCheckModel::IS_OUT_RULES_NO;
        $flag = '-';
        if ($data['is_out_rules'] == AlbumImgCheckModel::IS_OUT_RULES_NO) {
            $is_out_rules = AlbumImgCheckModel::IS_OUT_RULES_YES;
            $flag = "+";
        }
        Db::startTrans();
        try{
            $res = $model->save(['is_out_rules' => $is_out_rules], $where);
            if (!$res) {
                throw new Exception('更新图片失败');
            }
            $userM = new WechatUserModel();
            if ($flag == '+') {
                $res = $userM->where(['openid' => $open_id])->setInc('out_rules_times', 1);
                if (!$res) {
                    throw new Exception('更新用户信息失败');
                }
            } else {
                $res = $userM->where(['openid' => $open_id])->setDec('out_rules_times', 1);
                if (!$res) {
                    throw new Exception('更新用户信息失败');
                }
            }
            Db::commit();
            return json(['code' => 1, 'msg' => 'success']);
        }catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 0, 'error' => $e->getMessage()]);
        }
    }

    /**
     * 确认选择的图片为违规图片
     * 并给发送图片的微信用户的
     * 信用打分
     *
     * @return \think\response\Json
     */
    public function lock_all() {
        $data = $_REQUEST;
        if (empty($data) || empty($data['data'])) {
            return json(['code' => 0, 'error' => '请先选择']);
        }
        $model = new AlbumImgCheckModel();
        $ids = []; // id list
        $open_ids = []; // 根据open_id 计数该用户的违规次数
        foreach ($data['data'] as $item) {
            $ids[] = $item['id'];
            if ($item['is_out_rules'] == AlbumImgCheckModel::IS_OUT_RULES_NO) {
                if (isset($open_ids[$item['open_id']])) {
                    $open_ids[$item['open_id']] +=1;
                } else {
                    $open_ids[$item['open_id']] = 1;
                }
            }
        }
        Db::startTrans();
        try{
            $model->where('id', 'IN', $ids)->update(['is_out_rules' => AlbumImgCheckModel::IS_OUT_RULES_YES]);
            $userM = new WechatUserModel();
            foreach ($open_ids as $key => $val) {
                $res = $userM->where(['openid' => $key])->setInc('out_rules_times', $val);
                if (!$res) {
                    throw new Exception('更新用户信息失败');
                }
            }
            Db::commit();
            return json(['code' => 1, 'msg' => 'success']);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 0, 'error' => $e->getMessage()]);
        }
    }

}